app.controller('NewRoleModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var role_name, role_remark;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {
                    role_name = $scope.oo.name;
                    role_remark = $scope.oo.des;                                         
                                      
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'newroleModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"admin-role/create",
                        {
                            'name': role_name,
                            'remark': role_remark,                           
                            'token':$localStorage.token             

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=='创建成功'){
                        alert(data.msg);
                        $window.location.reload('app/sys/role');                      

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        alert(data);
                    })
                });
            };
        }])

app.controller('EditRoleModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$localStorage', '$state', '$location',
        function ($scope, $modal, $log, $http, $window, $localStorage, $state, $location) {
            $scope.items = ['item1', 'item2', 'item3'];
            //console.log($state.params);
            var role_name, role_remark;
            var rolename,roleremark,roleid;
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {
                $scope.oo = {};
                $scope.oo.name = role_name;
                $scope.oo.des = role_remark;                 

                $scope.items = items;
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {
                    rolename = $scope.oo.name;
                    roleremark = $scope.oo.des;                   
                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };

            $scope.editrole = function(obj){
                //console.log(obj);
                role_name = obj.name;
                roleid = obj.id;
                role_remark = obj.remarks;               
                $scope.open();

            }
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'editroleModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });
                modalInstance.result.then(function (selectedItem) {
                    
                    $http.post(
                        $scope.app.backendurl+"admin-role/update",
                        {
                            'id': roleid,
                            'name': rolename,
                            'remark': roleremark,                           
                            'token':$localStorage.token                         

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="修改成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/role');

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }

                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        alert(data);
                    })
                });
            };
        }])


app.controller('DelRoleModalCtrl', ['$scope', '$modal', '$log', '$http', '$window', '$location','$localStorage',
        function ($scope, $modal, $log, $http, $window, $location,$localStorage) {

            $scope.items = ['item1', 'item2', 'item3'];
            var role_id;            
            var ModalInstanceCtrl = function ($scope, $modalInstance, items) {

                $scope.oo = {};

                $scope.items = items;               
                $scope.selected = {
                    item: $scope.items[0]
                };
                $scope.ok = function () {

                    $modalInstance.close($scope.selected.item);
                };

                $scope.cancel = function () {
                    $modalInstance.dismiss('cancel');
                };
            };


            $scope.delrole = function (aid) {
                //console.log(aid);
                role_id = aid;             
                $scope.open();
            }

            $scope.open = function (size) {

                var modalInstance = $modal.open({
                    templateUrl: 'delroleModalContent.html',
                    controller: ModalInstanceCtrl,
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;

                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {
                    $http.post(
                        $scope.app.backendurl+"admin-role/delete",
                        {
                           
                            'id': role_id,                             
                            'token':$localStorage.token                   

                        }
                    ).success(function (data) {
                      //console.log(data);
                      if(data.code==0&&data.msg=="删除成功"){
                        alert(data.msg);
                        $window.location.reload('app/sys/role');

                      }
                      if(data.code==1){
                        alert(data.msg)
                      }
                       if(data.code==20001){
                         $location.path('access.signin');
                       }
                    }).error(function (data) {
                        alert(data);
                    })


                });
            };
        }])